<template>
  <a-modal
    title="新增问卷表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="标题"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [{required: true, message: '请输入标题！'}]}]" />
        </a-form-item>
        <a-form-item
          label="价格"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入价格" type='Number' v-decorator="['price', {rules: [{required: true, message: '请输入价格！'}]}]" />
        </a-form-item>
        <a-form-item
          label="问卷类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择问卷类型" v-decorator="['questionnaireCategoryId', {rules: [{ required: true, message: '请选择问卷类型！' }]}]">
            <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="是否甄别问卷"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group placeholder="请选择是否甄别问卷" v-decorator="['isScreening',{rules: [{ required: true, message: '请选择是否甄别问卷！' }]}]" >
            <a-radio v-for="(item,index) in whetherData" :key="index" :value="item.code">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="截止时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker
            style="width: 100%"
            placeholder="请选择截止时间"
            v-decorator="['endTime',{rules: [{ required: true, message: '请选择截止时间！' }]}]"
            @change="endTimeOnChange"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { questionnaireAdd } from '@/api/modular/main/questionnaire/questionnaireManage'
  import { questionnaireCategoryList } from '@/api/modular/main/questionnairecategory/questionnaireCategoryManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        typeData: [],
        whetherData: [],
        endTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        this.whetherData = this.$options.filters['dictData']('whether')
        questionnaireCategoryList().then(res => {
          this.typeData = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              isScreening: '0'
            }
          )
        }, 100)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.endTime = this.endTimeDateString || null
            console.log(values)
            questionnaireAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      endTimeOnChange(date, dateString) {
        this.endTimeDateString = dateString
      },
      handleCancel () {
        this.endTimeDateString = ''
        this.form.getFieldDecorator('endTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
